package de.bensoft.jrail.client.ui.trackplan.blockadmin;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import javax.inject.Inject;

import de.bensoft.jrail.business.service.TrackplanService;
import de.bensoft.jrail.client.common.JavaFXController;
import de.bensoft.jrail.client.ui.common.UIConstants;
import de.bensoft.jrail.client.ui.trackplan.segments.BlockTrackSegment;

@JavaFXController(fxml="BlockAdmin.fxml", css=UIConstants.BASIC_CSS)
public class BlockAdminController {
	
	@Inject
	TrackplanService tpService;
	
	private BlockTrackSegment segment;
	public  BlockTrackSegment getSegment() { return segment; }
	public void setSegment(BlockTrackSegment segment) { this.segment = segment; load(); }

	// --------------------------------------------------------------------------------
	// FXML
	// --------------------------------------------------------------------------------
	@FXML
	private TextField tfName;
	
	@FXML
	private void onOK(ActionEvent ev) {
		save();
		((Stage)((Button)ev.getTarget()).getScene().getWindow()).close();
	}
	
	@FXML
	private void onCancel(ActionEvent ev) {
		((Stage)((Button)ev.getTarget()).getScene().getWindow()).close();
	}
	
	@FXML
	private void initialize() {
	}
	
	// --------------------------------------------------------------------------------
	// DATA
	// --------------------------------------------------------------------------------
	private void load() {
		tfName.setText(segment.getSegmentData().getBlockData().getName());	
	}
	
	private void save() {
		segment.getSegmentData().getBlockData().setName(tfName.textProperty().get());
		segment.nameProperty().set(tfName.textProperty().get());
		
		tpService.saveTrackplanSegment(segment.getSegmentData());
	}
}
